package com.feisuanyz.lyweb01.service.impl;

import com.feisuanyz.lyweb01.dao.ProductMapper;
import com.feisuanyz.lyweb01.pojo.domain.ProductDO;
import com.feisuanyz.lyweb01.pojo.dto.ProductDTO;
import com.feisuanyz.lyweb01.pojo.query.ProductQuery;
import com.feisuanyz.lyweb01.pojo.vo.PageResult;
import com.feisuanyz.lyweb01.service.ProductService;
import java.lang.Boolean;
import java.lang.Override;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.feisuanyz.lyweb01.exception.BusinessException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import java.util.Map;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.feisuanyz.lyweb01.constant.ResultCodeConstant;
import java.util.Date;

/**
 * 产品中心的实现
 *
 * @author deng
 * @date 2025-02-26 11:57:37
 */
@Service
public class ProductServiceImpl implements ProductService {

    @Autowired
    private ProductMapper productMapper;

    @Override
    public PageResult<ProductDO> productList(ProductQuery productQuery) {
        try {
            IPage<ProductDO> page = new Page<>(productQuery.getPageIndex(), productQuery.getPageSize());
            IPage<ProductDO> productPage = productMapper.selectPage(page, Wrappers.emptyWrapper());
            PageResult<ProductDO> pageResult = new PageResult<>(productPage);
            return pageResult;
        } catch (Exception e) {
            throw new BusinessException(ResultCodeConstant.CODE_999999, ResultCodeConstant.CODE_999999_MSG);
        }
    }

    @Override
    public ProductDO productInfo(ProductQuery productQuery) {
        try {
            ProductDO productDO = productMapper.selectById(productQuery.getProductId());
            return productDO;
        } catch (Exception e) {
            throw new BusinessException(ResultCodeConstant.CODE_999999, ResultCodeConstant.CODE_999999_MSG);
        }
    }

    @Override
    public List<ProductDO> searchProduct(ProductQuery productQuery) {
        try {
            QueryWrapper<ProductDO> queryWrapper = Wrappers.query(new ProductDO());
            queryWrapper.like("product_name", productQuery.getKeyword());
            List<ProductDO> productList = productMapper.selectList(queryWrapper);
            return productList;
        } catch (Exception e) {
            throw new BusinessException(ResultCodeConstant.CODE_999999, ResultCodeConstant.CODE_999999_MSG);
        }
    }

    @Override
    public Boolean addProduct(ProductDTO productDTO) {
        try {
            ProductDO productDO = new ProductDO();
            productDO.setProductName(productDTO.getProductName());
            productDO.setProductDescription(productDTO.getProductDescription());
            productDO.setProductImageUrl(productDTO.getProductImageUrl());
            productDO.setCreateBy(productDTO.getCreateBy());
            productDO.setCreateTime(new Date());
            int result = productMapper.insert(productDO);
            return result > 0;
        } catch (Exception e) {
            throw new BusinessException(ResultCodeConstant.CODE_999999, ResultCodeConstant.CODE_999999_MSG);
        }
    }

    @Override
    public Boolean updateProduct(ProductDTO productDTO) {
        try {
            ProductDO productDO = new ProductDO();
            productDO.setProductId(productDTO.getProductId());
            productDO.setProductName(productDTO.getProductName());
            productDO.setProductDescription(productDTO.getProductDescription());
            productDO.setProductImageUrl(productDTO.getProductImageUrl());
            productDO.setUpdateBy(productDTO.getUpdateBy());
            productDO.setUpdateTime(new Date());
            int result = productMapper.updateById(productDO);
            return result > 0;
        } catch (Exception e) {
            throw new BusinessException(ResultCodeConstant.CODE_999999, ResultCodeConstant.CODE_999999_MSG);
        }
    }

    @Override
    public Boolean deleteProduct(ProductDTO productDTO) {
        try {
            int result = productMapper.deleteById(productDTO.getProductId());
            return result > 0;
        } catch (Exception e) {
            throw new BusinessException(ResultCodeConstant.CODE_999999, ResultCodeConstant.CODE_999999_MSG);
        }
    }
}
